
const util = require('../../utils/util.js');
Page({
  data: {
    imagesBaseUrl: util.imagesBaseUrl,
    // avatar: 'http://124.220.3.30:9000/miniapp-xjy-dev/public/fenlei/youerzhaohu.png',
    avatar: '',
    // lifePhotos: ['public/fenlei/youerzhaohu.png', 'public/fenlei/title.png'],
    lifePhotos: [],
    // certificate: ['public/fenlei/youerzhaohu.png', 'public/fenlei/title.png'],
    certificate: [],
    tempFilePaths: [],
  },
  doChooseImage1() {
    const _this = this;
    wx.chooseImage({
      count: 1,
      success(res) {
        console.log(res)
        _this.setData({
          avatar: ''
        })
        _this.doUpLoadImage(res.tempFilePaths, 'avatar')
      }
    })
  },
  doDel1() {
    const _this = this;
    wx.showModal({
      title: '温馨提示',
      content: '确定删除吗？',
      confirmColor: '#F08302',
      showCancel: true,
      success(res) {
        if (res.confirm) {
          _this.setData({
            avatar: ''
          })
        }
      }
    })
  },
  doChooseImage2() {
    const _this = this;
    wx.chooseImage({
      count: 9,
      success(res) {
        console.log(res)
        _this.doUpLoadImage(res.tempFilePaths, 'lifePhotos')
      }
    })
  },
  doDel2(e) {
    const _this = this, data = e.currentTarget.dataset;
    console.log(data)
    wx.showModal({
      title: '温馨提示',
      content: '确定删除吗？',
      confirmColor: '#F08302',
      showCancel: true,
      success(res) {
        if (res.confirm) {
          _this.data.lifePhotos.splice(data.index, 1)
          _this.setData({
            lifePhotos: _this.data.lifePhotos
          })
        }
      }
    })
  },
  doChooseImage3() {
    const _this = this;
    wx.chooseImage({
      count: 9,
      success(res) {
        console.log(res)
        _this.doUpLoadImage(res.tempFilePaths, 'certificate')
      }
    })
  },
  doDel3(e) {
    const _this = this, data = e.currentTarget.dataset;
    console.log(data)
    wx.showModal({
      title: '温馨提示',
      content: '确定删除吗？',
      confirmColor: '#F08302',
      showCancel: true,
      success(res) {
        if (res.confirm) {
          _this.data.certificate.splice(data.index, 1)
          _this.setData({
            certificate: _this.data.certificate
          })
        }
      }
    })
  },
  doUpLoadImage(tempFilePaths, type) {
    const _this = this;
    wx.showLoading({
      title: '文件上传中...',
    })
    tempFilePaths.forEach((item) => {
      wx.getFileSystemManager().readFile({
        filePath: item,
        encoding: "base64",
        success(res2) {
          const sss = res2.data

          util.https('comm/imageUpload', { imageBase64: sss }, (res) => {
            console.log(res)
            // list.push(res.imageUrl)
            wx.showToast({
              title: '上传成功',
              icon: 'success',
              duration: 3000
            })
            if (type == 'avatar') {
              _this.setData({
                avatar: res.imageUrl
              })
            } else if (type == 'lifePhotos') {
              _this.data.lifePhotos.push(res.imageUrl)
              _this.setData({
                lifePhotos: _this.data.lifePhotos
              })
            }
            else if (type == 'certificate') {
              _this.data.certificate.push(res.imageUrl)
              _this.setData({
                certificate: _this.data.certificate
              })
            }
          }, (res) => {
            console.log(res)
            wx.showModal({
              title: '温馨提示',
              content: '上传失败',
              confirmColor: '#F08302',
              showCancel: false,
              success(res) {
              }
            })
          }, () => {

          })
        }
      })
    })
    wx.hideLoading()
  },

  doNext() {
    let data = this.data;
    if (!data.avatar) {
      wx.showModal({
        title: '温馨提示',
        content: '请上传头像图片！',
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
      return
    }
    try {
      let storageData = wx.getStorageSync('bmhetong');
      storageData.certificate = data.certificate.join();
      storageData.lifePhotos = data.lifePhotos.join();
      storageData.avatar = data.avatar;
      wx.setStorageSync('bmhetong', storageData);
      wx.navigateTo({ url: '/page5/bmhetong2/index' })
    } catch (e) { }

  },
  doBack() {
    wx.navigateBack({
      delta: 1
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  }
  
})